package model.admin;

import ecom.beans.ProductBean;
import ecom.beans.EcomAdminRemote;
import ecom.shell.ShellRemote;
import java.util.Collection;
import java.util.Map;
import model.AbstractPage;

/**
 *
 * @author Sebastien
 */
public class ProductManagement extends AbstractPage {

    private ProductBean product;
    private Collection categories;
    private int selectedCategory;

    private static final String xsl = "C://JAVA//wkspace//ecom//ecom-war//web//XSL/admin/product.xsl";
    private static final String formPage = "admin/product/form.jsp";
    private static final String listPage = "admin/list.jsp";

    public String addOrUpdate() throws Exception {
        String[] id = (String[]) getParameters().get("id");
        String[] requestAction = (String[]) getParameters().get("action");
        EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");

        if ((id != null && !"0".equalsIgnoreCase(id[0])) || (product != null && product.getProductId() != 0)) {

            // update
            if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
                admin.updateProduct(product.getProductId(), product.getProductName(), product.getDescription(), selectedCategory);
            } // affichage du formulaire pré-rempli
            else {
                product = admin.getProductById(Integer.valueOf(id[0]));
                categories = admin.getCategories();
                this.setPage(formPage);
                return SUCCESS;
            }
        } else {

            // insert
            if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
                admin.createProduct(product.getProductName(), product.getDescription(), selectedCategory);
            } // affichage du formulaire pré-rempli
            else {
                categories = admin.getCategories();
                this.setPage(formPage);
                return SUCCESS;
            }
        }
        return list();
    }

    public String list() throws Exception {
        ShellRemote shell = (ShellRemote) getSession().get("shell");
        this.setDataToPrint(shell.executeCommand("product"));
        this.setXslPath(xsl);
        this.setPage(listPage);
        return SUCCESS;
    }

    public String delete() throws Exception {
        String[] id = (String[]) getParameters().get("id");

        if (id != null && !"".equalsIgnoreCase(id[0])) {
            EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");
            admin.removeProduct(Integer.valueOf(id[0]));
        }
        return list();
    }

    @Override
    public void validate() {
        String[] requestAction = (String[]) getParameters().get("action");
        if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
            this.setPage(formPage);
            EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");
            categories = admin.getCategories();

            if (product.getProductName().length() == 0) {
                this.addFieldError("product.productName", "Préciser le nom");
            }
            if (product.getDescription().length() == 0) {
                this.addFieldError("product.description", "Préciser la description");
            }
            if (selectedCategory == 0) {
                this.addFieldError("selectedCategory", "Préciser la catégorie");
            }
        }
    }

    @Override
    public String execute() throws Exception {
        message = "ProductManagement()";
        return SUCCESS;
    }

    public Collection getCategories() {
        return categories;
    }

    public void setSelectedCategory(int selectedCategory) {
        this.selectedCategory = selectedCategory;
    }

    public ProductBean getProduct() {
        return product;
    }

    public void setProduct(ProductBean product) {
        this.product = product;
    }
}